//
//  TTFillInLogisticsViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/16.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTFillInLogisticsViewController.h"
#import "TTSendExpressListModel.h"
#import <UIButton+YYWebImage.h>
#import "TTRecyclingSuccessViewController.h"

@interface TTFillInLogisticsViewController ()

@property (nonatomic, strong) UITextField *numTF;
@property (nonatomic, strong) UIButton *selectbtn;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation TTFillInLogisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"去寄送";
    [self setupSubviews];
    
}

/// 点击了按钮
- (void)clickBtn:(UIButton *)sender
{
    sender.selected = YES;
    _selectbtn.selected = NO;
    _selectbtn = sender;
    
    _selectIndex = sender.tag - 222 + 1;
    
}

/// 提交
- (void)bottomBtnClick
{
    if (_selectIndex == 0) {
        [self.view makeToast:@"请选择快递公司"];
        return;
    }
    if ([_numTF.text removeFirstAndLastBlank].length == 0) {
        [self.view makeToast:@"请填写运单号"];
        return;
    }
    
    NSDictionary *dic = @{
        @"SendMethod" : @1,
        @"Express" : @(self.selectIndex),
        @"ExpressNumber" : [self.numTF.text removeFirstAndLastBlank],
        @"RealName" : [TTLoginSingle sharedSingle].loginModel.RealName,
        @"Reclaim_InformationId" : self.Reclaim_InformationId
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:recyclingSend_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        /// 提交成功
        TTRecyclingSuccessViewController *sVC = [[TTRecyclingSuccessViewController alloc] init];
        sVC.Reclaim_InformationId = self.Reclaim_InformationId;
        [self.navigationController pushViewController:sVC animated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
    
}

- (void)setupSubviews
{
    UILabel *label = [UILabel labelWithTitle:@"快递公司" color:UIColorHex(#333333) font:[UIFont boldSystemFontOfSize:15]];
    label.frame = CGRectMake(15, 25, 200, 30);
    [self.view addSubview:label];
    
    CGFloat w = 87, h = 68;
    CGFloat space = (self.view.width - 30 - self.expressListArray.count * w) / (self.expressListArray.count - 1);
    space = space > 40 ? 40 : (space < 10 ? 10 : space);
    
    for (int i = 0; i < self.expressListArray.count; i++) {
        
        TTSendExpressListModel *model = self.expressListArray[i];
        
        UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(15 + (w + space) * i, label.bottom + 5, w, h) image:[UIImage imageNamed:@"shangmen"] title:model.Express_Text titleColor:UIColorHex(#333333) titleFont:[UIFont systemFontOfSize:12] backgroundColor:nil target:self action:@selector(clickBtn:)];
        btn.tag = 222 + i;
        [btn setTitleColor:UIColorHex(#9748EC) forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#F7F7F7)] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#F9F4FF)] forState:UIControlStateSelected];
        @weakify(btn)
        [btn setImageWithURL:[NSURL URLWithString:model.Icon] forState:UIControlStateNormal placeholder:nil options:YYWebImageOptionUseNSURLCache completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
            @strongify(btn)
            CGFloat offset = 10.0f;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height-offset/2, 0);
            btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
            
        }];
        [self.view addSubview:btn];
        
    }
    
    UILabel *label1 = [UILabel labelWithTitle:@"运单号" color:UIColorHex(#333333) font:[UIFont boldSystemFontOfSize:15]];
    label1.frame = CGRectMake(15, label.bottom + h + 30, 200, 30);
    [self.view addSubview:label1];
    
    _numTF = [[UITextField alloc] initWithFrame:CGRectMake(40, label1.bottom, 200, 38)];
    _numTF.placeholder = @"请填写运单号";
    _numTF.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_numTF];
    
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, _numTF.bottom, 230, 1)];
    lineView.backgroundColor = UIColorHex(#999999);
    [self.view addSubview:lineView];
    
    
    
    UIView *btnBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bottom - StatusBarAndNavBar_Height - Tabbar_Height - 40, self.view.width, StatusBarAndNavBar_Height)];
    btnBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btnBgView];
    UIButton *bottomBtn = [UIButton createButtonWithTitle:@"提交" titleColor:UIColor.whiteColor titleFont:[UIFont systemFontOfSize:17] backgroundColor:UIColorHex(#9343EB) cornerRadius:20 target:self action:@selector(bottomBtnClick)];
    bottomBtn.frame = CGRectMake(16, 10, self.view.width - 32, 40);
    [btnBgView addSubview:bottomBtn];
    
    
    
}

@end
